<?php
/**
 * User: 苏翔
 * Date: 2024/8/16 14:07
 */

namespace App\Dao;


use App\Model\HscoinConfModel;
use Hyperf\Di\Annotation\Inject;
use App\Model\FundConfModel;
use Hyperf\DbConnection\Db;

/**
 * @todo json
 */
class HscoinConfDao extends BaseDao
{
    #[Inject]
    protected HscoinConfModel $model;

    function save($data)
    {
        $rt = 0;
        foreach ($data as $key => $val) {
            $rs = $this->model->updateOrCreate(['conf_key' => $key], ['conf_key' => $key, 'conf_val' => $val]);
            $rt += $rs->affectedRows;
        }
        return $rt;
    }

    function getAll($keys)
    {
        $table = $this->model->getTable();
        $rs = Db::table($table)
            ->whereIn('conf_key', (array)$keys)
            ->select(['conf_key', 'conf_val'])
            ->get()
            ->pluck('conf_val', 'conf_key')
            ->toArray();
        return $rs;
    }

    function get($key, $default = null)
    {
        $isReturnArr = !is_string($key);
        $keys = array_filter(array_map('trim', (array)$key));
        $rs = $this->getAll($key);
        return $isReturnArr ? $rs : $rs[$keys[0]] ?? $default;

    }
}